;(function(){
    $('.js-example-basic-single').select2();
    $('#member_ship_id').select2({placeholder:"请选择归属会籍"});
    $('#gym_card_name_select').select2({placeholder:"--请选择卡种--"});
//功能按钮监听事件
    $(".menu_buttons .menu_3").on("click",function(){
        var node=$(this);
        $("#myModalTitle").text(node.text());
        if(node.text().indexOf("入会")>-1){
            $("#myModalBody").load(node.attr("data-url"),function(){
                $("#myModal").modal("show");
            });
            return false;
        }
        else if(node.text().indexOf("详情")>-1){
            var $ids=[];
            $("#data-table input[name='ids']:checked").each(function(){
                $ids.push($(this).val());
            });
            if($ids.length!=1){
                toastr['error']("请选择一项","");
                return false;
            }else{
            	var shop_id=  $("#data-table").DataTable().row(rowIndex-1).data().shop_id;
                $("#myModalBody").load(node.attr("data-url"),{"id":$ids[0],"shop_id":shop_id},function(){
                    $("#myModal").modal("show");
                });
            }
            return false;
        }
        else if(node.text().indexOf("升级")>-1){
            var $ids=[];
            $("#data-table input[name='ids']:checked").each(function(){
                $ids.push($(this).val());
            });
            if($ids.length!=1){
                toastr['error']("请选择一项","");
                return false;
            }else{
                var rowIndex=$("#data-table input[name='ids']:checked").prev().text();
                var order_id=  $("#data-table").DataTable().row(rowIndex-1).data().order_id;
                if(order_id==null||order_id==""||order_id<1){
                    toastr['error']("此会员没有开卡，请先开卡","");
                    return false;
                }
                $("#myModalBody").load(node.attr("data-url"),{"order_id":order_id},function(){
                    $("#myModal").modal("show");
                });
            }
            return false;
        }
        else if(node.text().indexOf("退款")>-1){
            var $ids=[];
            $("#data-table input[name='ids']:checked").each(function(){
                $ids.push($(this).val());
            });
            if($ids.length!=1){
                toastr['error']("请选择一项","");
                return false;
            }else{
                var rowIndex=$("#data-table input[name='ids']:checked").prev().text();
                var order_id=  $("#data-table").DataTable().row(rowIndex-1).data().order_id;
                if(order_id==null||order_id==""||order_id<1){
                    toastr['error']("此会员没有开卡，请先开卡","");
                    return false;
                }
                var gym_card_type=  $("#data-table").DataTable().row(rowIndex-1).data().gym_card_type;
                $("#myModalBody").load(node.attr("data-url"),{"order_id":order_id,"gym_card_type":gym_card_type},function(){
                    $("#myModal").modal("show");
                });
            }
            return false;
        }
        else if(node.text().indexOf("退会")>-1){
            var $ids=[];
            $("#data-table input[name='ids']:checked").each(function(){
                $ids.push($(this).val());
            });
            if($ids.length!=1){
                toastr['error']("请选择一项","");
                return false;
            }else{
                var rowIndex=$("#data-table input[name='ids']:checked").prev().text();
                var order_id=  $("#data-table").DataTable().row(rowIndex-1).data().order_id;
                if(order_id==null||order_id==""||order_id<1){
                    toastr['error']("此会员没有开卡，请先开卡","");
                    return false;
                }

                $.post(
                    "/Gym/GymMember/has_private_lesson",
                    {"id":$ids[0]},
                    function(data){
                        if(data.flag){
                            var gym_card_type=  $("#data-table").DataTable().row(rowIndex-1).data().gym_card_type;
                            $("#myModalBody").load(node.attr("data-url"),{"order_id":order_id,"gym_card_type":gym_card_type},function(){
                                $("#myModal").modal("show");
                            });
                        }else{
                            toastr['error']("失败",data.msg);
                            return false;
                        }
                    }
                );
            }
            return false;
        }
        else if(node.text().indexOf("续卡")>-1){
            var $ids=[];
            $("#data-table input[name='ids']:checked").each(function(){
                $ids.push($(this).val());
            });
            if($ids.length!=1){
                toastr['error']("请选择一项","");
                return false;
            }else{
                var rowIndex=$("#data-table input[name='ids']:checked").prev().text();
                var order_id=  $("#data-table").DataTable().row(rowIndex-1).data().order_id;
                if(order_id==null||order_id==""||order_id<1){
                    toastr['error']("此会员没有开卡，请先开卡","");
                    return false;
                }
                var gym_card_type=  $("#data-table").DataTable().row(rowIndex-1).data().gym_card_type;
                $("#myModalBody").load(node.attr("data-url"),{"order_id":order_id,"gym_card_type":gym_card_type},function(){
                    $("#myModal").modal("show");
                });
            }
            return false;
        }
        else if(node.text().indexOf("换卡")>-1){
            var $ids=[];
            $("#data-table input[name='ids']:checked").each(function(){
                $ids.push($(this).val());
            });
            if($ids.length!=1){
                toastr['error']("请选择一项","");
                return false;
            }else{
                var rowIndex=$("#data-table input[name='ids']:checked").prev().text();
                var order_id=  $("#data-table").DataTable().row(rowIndex-1).data().order_id;
                if(order_id==null||order_id==""||order_id<1){
                    toastr['error']("此会员没有开卡，请先开卡","");
                    return false;
                }
                var gym_card_type=  $("#data-table").DataTable().row(rowIndex-1).data().gym_card_type;
                $("#myModalBody").load(node.attr("data-url"),{"order_id":order_id},function(){
                    $("#myModal").modal("show");
                });
            }
            return false;
        }
        else if(node.text().indexOf("转卡")>-1){
            var $ids=[];
            $("#data-table input[name='ids']:checked").each(function(){
                $ids.push($(this).val());
            });
            if($ids.length!=1){
                toastr['error']("请选择一项","");
                return false;
            }else{
                var rowIndex=$("#data-table input[name='ids']:checked").prev().text();
                var order_id=  $("#data-table").DataTable().row(rowIndex-1).data().order_id;
                if(order_id==null||order_id==""||order_id<1){
                    toastr['error']("此会员没有开卡，请先开卡","");
                    return false;
                }
                $("#myModalBody").load(node.attr("data-url"),{"order_id":order_id},function(){
                    $("#myModal").modal("show");
                });
            }
            return false;
        }
        else if(node.text().indexOf("请假")>-1){
            var $ids=[];
            $("#data-table input[name='ids']:checked").each(function(){
                $ids.push($(this).val());
            });
            if($ids.length!=1){
                toastr['error']("请选择一项","");
                return false;
            }else{
                var rowIndex=$("#data-table input[name='ids']:checked").prev().text();
                var order_id=  $("#data-table").DataTable().row(rowIndex-1).data().order_id;
                var order_status=  $("#data-table").DataTable().row(rowIndex-1).data().order_status;
                if(order_id==null||order_id==""||order_id<1){
                    toastr['error']("此会员没有开卡，请先开卡","");
                    return false;
                }
                if(order_status==3){
                    toastr['error']("此会员没有激活，无法请假","");
                    return false;
                }
                $("#myModalBody").load(node.attr("data-url"),{"order_id":order_id},function(){
                    $("#myModal").modal("show");
                });
            }
            return false;
        }
        else if(node.text().indexOf("销假")>-1){
            var $ids=[];
            $("#data-table input[name='ids']:checked").each(function(){
                $ids.push($(this).val());
            });
            if($ids.length!=1){
                toastr['error']("请选择一项","");
                return false;
            }else{
                var rowIndex=$("#data-table input[name='ids']:checked").prev().text();
                var order_id=  $("#data-table").DataTable().row(rowIndex-1).data().order_id;
                var order_status=$("#data-table").DataTable().row(rowIndex-1).data().order_status;
                if(order_id==null||order_id==""||order_id<1){
                    toastr['error']("此会员没有开卡，请先开卡","");
                    return false;
                }
                else if(order_status!=4){
                    toastr['error']("此会员没有请假！无需销假","");
                    return false;
                }
                $("#myModalBody").load(node.attr("data-url"),{"order_id":order_id},function(){
                    $("#myModal").modal("show");
                });
            }
            return false;
        }
        else if(node.text().indexOf("修改")>-1){
            var $ids=[];
            $("#data-table input[name='ids']:checked").each(function(){
                $ids.push($(this).val());
            });
            if($ids.length!=1){
                toastr['error']("请选择一项","");
                return false;
            }else{
                $("#myModalBody").load(node.attr("data-url"),{"id":$ids[0]},function(){
                    $("#myModal").modal("show");
                });
            }
            return false;
        }
        else if(node.text().indexOf("Step")>-1){
            $("#myModalBody").load(node.attr("data-url"),function(){
                $("#myModal").modal("show");
            });
            return false;
        }
        else if(node.text().indexOf("删除")>-1){
            var $ids=[];
            var url=node.attr("data-url");
            $("#data-table input[name='ids']:checked").each(function(){
                $ids.push($(this).val());
            });
            if($ids.length!=1){
                toastr['error']("请至少选择一项","");
                return false;
            }else{
                url=url+"/id/"+$ids;
                delete_dialog(url);
                return false;
            }
            return false;
        }
    });

///////////////////////数据表格////////////////////////////
//统一点击删除弹出对话框
    var userManage = {

        currentItem : null,
        fuzzySearch : false,
        getQueryCondition : function(data) {
            var param = {};
            //组装排序参数
            if (data.order&&data.order.length&&data.order[0]) {
                switch (data.order[0].column) {
                    case 1:
                        param.orderColumn = "id";
                        break;
                    case 2:
                        param.orderColumn = "name";
                        break;
                }
                param.orderDir = data.order[0].dir;
            }
            //组装查询参数
            param.name= $("#myform #name").val();
            param.card_no= $("#myform #card_no").val();
            param.mobile= $("#myform #mobile").val();
            param.card_type= $("#myform #card_type").val();
            param.shop_id = $("#myform #shop_id").val();
            param.api_version = $("#myform #api_version").val();

            //组装分页参数
            param.startIndex = data.start;
            param.pageSize = data.length;
            return param;
        },

    }

    var $table = $('#data-table');
    var _table = $table.dataTable($.extend(true,{},CONSTANT.DATA_TABLES.DEFAULT_OPTION, {
        ajax : function(data, callback, settings) {//ajax配置为function,手动调用异步查询
            //手动控制遮罩
            //封装请求参数
            var param = userManage.getQueryCondition(data);
            $("#excel_param").val($.param(param));
            loading();
            $.ajax({
                type: "GET",
                url: "/operation/employee/ajaxMember",
                cache : false,  //禁用缓存
                data: param,    //传入已封装的参数
                dataType: "json",
                success: function(result) {
                    close_loading();
                    //setTimeout仅为测试延迟效果
                    setTimeout(function(){
                        //异常判断与处理
                        if (result.errorCode) {
                            toastr['error']("查询失败errorCode:"+result.errorCode);
                            close_loading();
                            return;
                        }

                        //封装返回数据，这里仅演示了修改属性名
                        var returnData = {};
                        returnData.draw = data.draw;//这里直接自行返回了draw计数器,应该由后台返回
                        returnData.recordsTotal = result.total;
                        returnData.recordsFiltered = result.total;//后台不实现过滤功能，每次查询均视作全部结果
                        returnData.data = result.pageData;
                        //关闭遮罩
                        //调用DataTables提供的callback方法，代表数据已封装完成并传回DataTables进行渲染
                        //此时的数据需确保正确无误，异常判断应在执行此回调前自行处理完毕
                        callback(returnData);
                    },200);
                },
                error: function(XMLHttpRequest, textStatus, errorThrown) {
                    close_loading();
                    toastr['error']("查询失败");
                }
            });
        },
        columns: [
            CONSTANT.DATA_TABLES.COLUMN.CHECKBOX,
            {
                data: "card_no",
                render : CONSTANT.DATA_TABLES.RENDER.ELLIPSIS,//会显示省略号的列，需要用title属性实现划过时显示全部文本的效果
            },
            {
                data: "name",
                render : CONSTANT.DATA_TABLES.RENDER.ELLIPSIS,//会显示省略号的列，需要用title属性实现划过时显示全部文本的效果
            },
            {
                data: "mobile",
                render : CONSTANT.DATA_TABLES.RENDER.ELLIPSIS,//会显示省略号的列，需要用title属性实现划过时显示全部文本的效果
            },

            {
                data: "card_type",
                render : function(data,type, row, meta){
                    if(data==1){return "<span  class='label label-info'>手环</span>";}
                    else if(data==2){return "<sapn class='label label-warning'>卡片</span>";}
                    else if(data==3){return "<sapn class='label label-default'>数据手环</span>";}
                    else {return "<sapn class='label label-grape'>-</span>";}
                },
            },
            {
                data: "in_varifycation",
                render : function(data,type, row, meta){
                    if(data==1){return "<span  class='label label-info'>需要验证</span>";}
                    else {return "<sapn class='label label-grape'>不验证</span>";}
                },
            },
            {
                data: "in_varifycation_type",
                render : function(data,type, row, meta){
                    if(data==1){return "<span  class='label label-info'>指纹验证</span>";}
                    else if(data==2){return "<sapn class='label label-warning'>人脸认证</span>";}
                    else if(data==3){return "<sapn class='label label-default'>前台验证</span>";}
                    else {return "<sapn class='label label-grape'>其他</span>";}
                },
            },
            {
                data: "shop_name",
                render : CONSTANT.DATA_TABLES.RENDER.ELLIPSIS,//会显示省略号的列，需要用title属性实现划过时显示全部文本的效果
            },
            {
                data: "shop_id",
                visible:false,
            },
        ],
        "createdRow": function ( row, data, index ) {},
        "drawCallback": function( settings ) {
            // $(":checkbox[name='cb-check-all']",$wrapper).prop('checked', false);
            //默认选中第一行
            // $("tbody tr",$table).eq(0).click();
        }
    })).api();//此处需调用api()方法,否则返回的是JQuery对象而不是DataTables的API对象
    $("#myform  #btn-advanced-search").click(function(){
        _table.draw();
    });

    //行点击事件
    $("tbody",$table).on("click","tr",function(event) {
        $(this).addClass("active").siblings().removeClass("active");
        //获取该行对应的数据
        var item = _table.row($(this).closest('tr')).data();
    });
})()
